using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ResourceBlender.Core;

namespace ResourceBlender.Express {
    /// <summary>
    /// A resource used by one or more elements
    /// </summary>
    public class ObservableResource : RBResource, INotifyPropertyChanged {
        private ObservableSortedDictionary<string, RBTranslation> translations;

        /// <summary>
        /// Gets or sets a dictionary of translations for the resource sorted by the language's English name. In the format <code, translation>
        /// </summary>
        /// <value>A dictionary of the translations available.</value>
        public new ObservableSortedDictionary<string, RBTranslation> Translations {
            get { return translations; }
            set {
                translations = value;
                OnPropertyChanged("Translations");
            }
        }

        /// <summary>
        /// Initializes a new <see cref="ObservableResource"/>.
        /// </summary>
        public ObservableResource() {
            id = -1;
            comment = string.Empty;
            translations = new ObservableSortedDictionary<string, RBTranslation>(new Comparers.LanguageNameDictionaryComparer(Comparers.LanguageNameDictionaryComparer.CompareMode.Keys));
        }

        /// <summary>
        /// Initializes a new <see cref="ObservableResource"/>.
        /// </summary>
        public ObservableResource(RBResource resource) {
            this.id = resource.ID;
            this.comment = resource.Comment;
            translations = new ObservableSortedDictionary<string, RBTranslation>(new Comparers.LanguageNameDictionaryComparer(Comparers.LanguageNameDictionaryComparer.CompareMode.Keys));
            foreach(var t in resource.Translations) {
                translations.Add(t.Key, t.Value);
            }
        }

        /// <summary>
        /// Initializes a new <see cref="ObservableResource"/>.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="comment">The comment.</param>
        public ObservableResource(int id, string comment)
            : this() {
            this.id = id;
            this.comment = comment;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string strPropertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }

        public ObservableResource Clone() {
            ObservableResource res = new ObservableResource() {
                Comment = this.Comment,
                ID = this.ID
            };
            foreach(KeyValuePair<string, RBTranslation> translation in Translations) {
                res.Translations.Add(translation.Key, translation.Value.Clone());
            }
            return res;
        }

        public RBResource ToRBResource() {
            RBResource res = new RBResource() {
                Comment = this.Comment,
                ID = this.ID
            };
            foreach(KeyValuePair<string, RBTranslation> translation in Translations) {
                res.Translations.Add(translation.Key, translation.Value.Clone());
            }
            return res;
        }
    }
}
